package com.atguigu.crowd.funding.handler;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.atguigu.crowd.funding.entity.Admin;
import com.atguigu.crowd.funding.entity.ResultEntity;
import com.atguigu.crowd.funding.service.api.AdminService;
import com.atguigu.crowd.funding.util.CrowdFundingConstant;
import com.github.pagehelper.PageInfo;

@Controller
public class AdminHandler {

	@Autowired
	private AdminService adminService;
	
	
	/**
	 * 测试-获取全部数据
	 * @param md
	 * @return
	 */
	@RequestMapping("/admin/get/all")
	public String getAll(Model md){
		List<Admin> list= adminService.getAll();
		md.addAttribute("admins", list);
		return "admin-target";
	}
	
	/**
	 * 注销
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/do/logout")
	public String logout(HttpServletRequest request){
		
		//清除session
		HttpSession session=request.getSession();
		session.invalidate();
		
		//重定向ת
		return "redirect:/index.html";
	}
	
	/**
	 * 登录
	 * @param loginAcct
	 * @param userPswd
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping("/admin/do/login")
	public String doLogin(
			@RequestParam("loginAcct") String loginAcct,
			@RequestParam("userPswd") String userPswd,
			Model model,
			HttpSession session
			){
		Admin admin=adminService.login(loginAcct,userPswd);
		
		if(admin==null){
			
			model.addAttribute(CrowdFundingConstant.ATTR_NAME_MESSAGE, CrowdFundingConstant.MESSAGE_LOGIN_FAILED);
			return "admin-login";
		}
		
		session.setAttribute(CrowdFundingConstant.ATTR_NAME_LOGIN_ADMIN, admin);
		//重定向
		return "redirect:/admin/to/main.html";	
	}
	
	/**
	 * （admin）用户管理
	 */
	@RequestMapping("/admin/query/for/search")
	public String queryforSearch(
			@RequestParam(value="pageNum",defaultValue="1") Integer pageNum,
			@RequestParam(value="pageSize",defaultValue="5") Integer pageSize,
			@RequestParam(value="keyWord",defaultValue="") String keyWord,
			Model model
			){
		PageInfo<Admin> pageInfo = adminService.queryAdminByKeyWord(pageNum, pageSize, keyWord);
		System.out.println(keyWord);
		model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGEINFO, pageInfo);
		return "admin-page";
	}
	
	/**
	 * （批量）删除
	 * */
	@ResponseBody()
	@RequestMapping("/admin/batch/remove")
	public ResultEntity<String> batchRemove(
			@RequestBody List<Integer> adminidlist
			){
		try{
			adminService.batchDelect(adminidlist);			
			return ResultEntity.successWithoutData();
		}catch(Exception e){
			return ResultEntity.failed(null, e.getMessage());
		}
	}
/*	*//**
	 * 转到新增页面,这种直接跳转的方法不用在congtroller里写了，
	 * 直接在mvc配置文件里加<mvc:view-controller path="/admin/to/addview.html" view-name="admin-add"/>即可
	 * *//*
	@RequestMapping("/admin/to/addview")
	public String toAddView(){
	return "admin-add";
	}*/
	@RequestMapping("/admin/save")
	public String save(Admin admin){
	
		adminService.save(admin);
		//使用重定向
		return "redirect:/admin/query/for/search";
	}
	
	/**
	 * returned：admin用户更新页面
	 */
	@RequestMapping("/admin/to/editView")
	public String editView(
			@RequestParam("id") Integer adminid,
			 Model model){
		Admin admin=adminService.queryAdminById(adminid);
		model.addAttribute("admin", admin);
		
		//使用重定向
		return "admin-edit";
	}
	@RequestMapping("/admin/update")
	public String update(Admin admin,
			@RequestParam("pageNum") Integer pageNum){
	
		try {
			adminService.update(admin);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		//使用重定向
		return "redirect:/admin/query/for/search.html?pageNum="+pageNum;
	}
	
}
